<?

if (!isset($DATA['menus'])) {	// fetch ALL menus

	$_lang = stack('lang', context('lang'));

//debug(mc("menu.menus", array()));

	if (sizeof($DATA['menus'] = mc("menu.menus", array())) && sizeof($DATA['menu'] = mc("menu.menu.$_lang", array()))) return;

//debug($DATA['menus']);

	$DATA['menu'] = array();
	if (sizeof($DATA['menus'] = datafetch_array('db_list', array(
		'table' => tb('data'), 
		'where' => "id_data=".D_MENU." AND id_parent=0", 
		'order' => 'name', 'index' => 'id')))) {
			
		foreach($DATA['menus'] as $id => $v)
			$DATA['menu'][$v['name']] = array();

		if ($temp = datafetch_array('db_list', array(
			'table' => tb('data'), 
			'where' => "id_data=".D_MENU." AND id_parent!=0 AND ".dbAndL('attr', AA_VISIBLE)." AND (assoc IS NULL OR assoc IN ('','$_lang'))",
			'order' => 'id_index', 'index' => 'id')))
			foreach($temp as $id => $v)
				$DATA['menu'][$DATA['menus'][$v['id_parent']]['name']][$id] = $v;
		
		foreach($DATA['menu'] as $id => $v) 
			$DATA['menu'][$id] = array_mark_bounds($v);
	}

//debug($DATA['menus']);
//debug($DATA['menu']);

	mcSet("menu.menus", $DATA['menus'], MC_EXPIRE - mt_rand(2*60, 7*60));
	mcSet("menu.menu.$_lang", $DATA['menu'], MC_EXPIRE - mt_rand(2*60, 7*60));
}
